#include "BlockQueue.hpp"
using namespace BlockQueueModule;
class Task
{
public:
    Task(){}
    Task(int a,int b):x(a),y(b)
    {}
    ~Task()
    {}
    void Excute(){result = x+y;}
    int X(){return x;}
    int Y(){return y;}
    int Result(){return result;}
private:
    int x;
    int y;
    int result;
};
//int data = 0;
void* Consumer(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
    while(true)
    {
        Task t;
        //从队列获得数据
        bq->Pop(&t);
        printf("获得一个任务:%d+%d=?\n",t.X(),t.Y());
        //模拟处理任务的时间
        sleep(1);
        t.Excute();
        printf("处理完一个任务:%d+%d=%d\n",t.X(),t.Y(),t.Result());
    }
}
void* Productor(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)arg;
    while(true)
    {
        //发布一个任务
        int x = rand()%10+1;
        int y = rand()%20+1;
        Task t(x,y);
        printf("发布一个任务: %d+%d=?\n",x,y);
        bq->EnterQueue(t);
        sleep(1);
    }
}
int main()
{
    srand((unsigned int)time(nullptr)^getpid());
    BlockQueueModule::BlockQueue<Task>* bq = new BlockQueueModule::BlockQueue<Task>();
    pthread_t tid1;
    pthread_create(&tid1, nullptr, Consumer, (void*)bq);
    // pthread_t tid3;
    // pthread_create(&tid3, nullptr, Consumer, (void*)bq);
    // pthread_t tid4;
    // pthread_create(&tid4, nullptr, Consumer, (void*)bq);
    pthread_t tid2;
    pthread_create(&tid2, nullptr, Productor, (void *)bq);
    while(true);
    return 0;
}